package com.app;

import java.util.function.Consumer;

/**
 * 函数式options
 * 
 * @author 北京八维博大科技 薛建新
 * @date 2022年4月13日 上午9:18:33
 * @Copyright 北京八维博大科技<br/>
 *            参考 https://coolshell.cn/articles/21146.html
 */
public class Server {
	private String addr;
	private int port;
	private String protocol;
	private int timeout;
	private int maxConns;

	public Server(String addr, int port) {
		this.addr = addr;
		this.port = port;
	}

	@SafeVarargs
	public Server(String addr, int port, Consumer<Server>... options) {
		this.addr = addr;
		this.port = port;
		for (var o : options) {
			o.accept(this);
		}
	}

	public static Consumer<Server> protocol(String p) {
		return s -> s.protocol = p;
	}

	public static Consumer<Server> timeout(int timeout) {
		return s -> s.timeout = timeout;
	}

	public static Consumer<Server> maxConns(int maxConns) {
		return s -> s.maxConns = maxConns;
	}

	public String getAddr() {
		return addr;
	}

	public int getPort() {
		return port;
	}

	public String getProtocol() {
		return protocol;
	}

	public int getTimeout() {
		return timeout;
	}

	public int getMaxConns() {
		return maxConns;
	}

	@Override
	public String toString() {
		return String.format("Server [addr=%s, port=%s, protocol=%s, timeout=%s, maxConns=%s]", addr, port, protocol,
				timeout, maxConns);
	}

}
